package edu.dali.enterprise_test.controller.auth;

import com.github.pagehelper.PageHelper;
import edu.dali.enterprise_test.model.Pager;
import edu.dali.enterprise_test.model.vo.Dto;
import edu.dali.enterprise_test.model.vo.SysRole;
import edu.dali.enterprise_test.model.vo.UserInfo;
import edu.dali.enterprise_test.service.auth.AuthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserMgrController {

    @Autowired
    private AuthService authService;

//    @Autowired
//    private StoreService storeService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 用户管理界面
     * @return
     */
    @GetMapping("index")
    public String user() {
        return "user/index";
    }


    @RequestMapping("getUsers")
    @ResponseBody
    public Pager getUsers(int page , int limit) {
        Dto dto = new Dto();

        if (!StringUtils.isEmpty(request.getParameter("username"))) {
            dto.put("username" ,request.getParameter("username") );
        }
        if (!StringUtils.isEmpty(request.getParameter("name"))) {
            dto.put("name" , request.getParameter("name"));
        }
        PageHelper.startPage(page, limit);
        Pager p = authService.getUserInfoByPage(dto);
        return p;
    }

    @RequestMapping("detail/{uid}")
    public String detail(@PathVariable String uid , Model model) {
        Dto dto = new Dto();
        dto.put("uid" , uid);
        UserInfo userInfo = authService.selectUser(dto);
        model.addAttribute("userInfo" , userInfo);
        return "user/detail";
    }

    /**
     * 添加用户界面
     * @param model
     * @return
     */
    @RequestMapping("addUser")
    public String addUser(Model model) {
        List<SysRole> roles = authService.getAllRoles(null);
        model.addAttribute("roles" , roles);

//        List<Dto> stores = storeService.getAllStore();

//        model.addAttribute("stores" , stores);
        return "user/add";
    }

    /***
     * 添加用户处理程序
     * @param username
     * @param name
     * @param available
     * @param roles
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public Map<String , Object> add(String username , String name, String available , String storeno , @RequestParam(value = "roles[]") String[] roles) {
        Map<String , Object> map = new HashMap<>();
        UserInfo user = new UserInfo();
        user.setUsername(username);
        user.setName(name);
        user.setState(1);
        user.setStoreno(storeno);
        user.setRoles(new ArrayList<SysRole>());
        for (String roleid : roles) {
            SysRole role = new SysRole();
            role.setId(Long.parseLong(roleid));
            user.getRoles().add(role);
        }
        authService.addUser(user);
        map.put("code" , "0");
        map.put("msg" , "添加成功");
        return map;
    }

    /**
     * 修改用户信息界面
     * @param uid
     * @param model
     * @return
     */
    @RequestMapping("updateUser/{uid}")
    public String updateUser(@PathVariable String uid, Model model) {
        Dto dto = new Dto();
        dto.put("uid" , uid);
        UserInfo user = authService.selectUser(dto);
        model.addAttribute("user" , user);

        List<SysRole> roles = authService.getAllRoles(null);

        List<String> roleNames = new ArrayList<>();
        for (SysRole role : user.getRoles()) {
            roleNames.add(role.getRole());
        }

//        List<Dto> stores = storeService.getAllStore();

//        model.addAttribute("stores" , stores);
        model.addAttribute("roles" , roles);
        model.addAttribute("roleNames" , roleNames);
        return "user/update";
    }

    @PostMapping("update")
    @ResponseBody
    public Map<String , String> update(Long uid, String username , String name ,String storeno, String available ,@RequestParam(value = "roles[]") String[] roles) {
        Map<String , String> map = new HashMap<>();

        UserInfo user = new UserInfo();
        user.setUid(uid);
        user.setUsername(username);
        user.setName(name);

        user.setStoreno(storeno);
        user.setState(1);
        user.setRoles(new ArrayList<SysRole>());
        for (String roleid : roles) {
            SysRole role = new SysRole();
            role.setId(Long.parseLong(roleid));
            user.getRoles().add(role);
        }
        authService.updateUser(user);
        map.put("code" , "0");
        map.put("msg" , "修改成功");
        return map;
    }

    @PostMapping("delete/{uid}")
    @ResponseBody
    public Map<String , String> delete(@PathVariable Long uid) {
        Map<String , String> map = new HashMap<>();

        authService.deleteUser(uid);
        map.put("code" , "0");
        map.put("msg" , "删除成功");
        return map;
    }
}
